﻿using DataServer.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay
{
    public partial class payfileupload : System.Web.UI.Page
    {
        public DataTable dtFile { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //先将控件隐藏
            this.FileUpload1.Attributes.Add("style", "display:none");
            //监视属性变化，此时主要用于捕捉文件路径的写入值
            this.FileUpload1.Attributes.Add("onpropertychange", "UploadOnpropertychange()");
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                //取出所选文件的本地路径  
                string fullFileName = this.FileUpload1.PostedFile.FileName;
                //从路径中截取出文件名  
                string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
                txtFileName.Text = fileName;
                //限定上传文件的格式  
                string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
                if (type == "xls" || type == "xlsx" || type == "csv" || type == "txt")
                {
                    //将文件保存在服务器中根目录下的files文件夹中  
                    string path = Server.MapPath("/files");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    string saveFileName = Server.MapPath("/files") + "\\" + fileName;
                    FileUpload1.PostedFile.SaveAs(saveFileName);

                    //向数据库中存储相应通知的附件的目录  
                    try
                    {
                        DataTable dt = ToDataUtils.GetDataTableByType(saveFileName, type);
                        dtFile = dt;

                        Server.Transfer("batchpay.aspx?action=fileupload", true);
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('上传的文件格式不正确!');</script>");
                    }
                    finally
                    {
                        //File.Delete(saveFileName);
                    }

                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请选择正确的格式');</script>");
                }
            }
            catch (Exception err)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + err.Message + "');</script>");
            }
        }
    }
}